/*
 * Copyright (C) 2009 TeamCity Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cdmdotnet.android.teamcity;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.cdmdotnet.android.teamcity.model.TCBuildProxy;
import com.cdmdotnet.android.teamcity.model.TCBuildTypeProxy;
import com.cdmdotnet.android.teamcity.model.TCProjectProxy;
import com.cdmdotnet.android.teamcity.R;

/**
 * Created by IntelliJ IDEA.
 * User: xfeldman
 * Date: Sep 1, 2009
 * Time: 2:14:54 AM
 * To change this template use File | Settings | File Templates.
 */
public class BuildsListPage extends ListActivity {

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

         final TCBuildTypeProxy builds = new TCBuildTypeProxy(getIntent());
         final ArrayAdapter<TCBuildProxy> adapter = new ArrayAdapter<TCBuildProxy>(this, 
                 android.R.layout.simple_list_item_1, builds.getBuilds());
         setListAdapter (adapter);

         // Set custom title
         getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.i_title_no_button);

         // Load project from HTTP asynchronously
         new AsyncLoader() {
			@Override
			protected void load() throws ClientProtocolException, IOException,
					JSONException {
					builds.load();
			}
			@Override
			public void onLoadingFinished(int status, String errMessage) {
                switch (status) {
                case LOADING_FAILED:
                    Toast.makeText(BuildsListPage.this, errMessage, Toast.LENGTH_LONG).show();
                    BuildsListPage.this.finish();
                case LOADING_SUCCEDED:
                       adapter.notifyDataSetChanged();
              			((TextView)findViewById(R.id.title)).setText(
              					getIntent().getStringExtra(TCProjectProxy.NAME) + ":" + getIntent().getStringExtra(TCBuildTypeProxy.NAME));
                }
                findViewById(R.id.progress).setVisibility(View.GONE);
			}
			@Override
			public void onLoadingStarted() {}
		}.asyncLoad();
     }

     /* (non-Javadoc)
      * @see android.app.ListActivity#onListItemClick(android.widget.ListView, android.view.View, int, long)
      */
     @Override
     protected void onListItemClick(ListView l, View v, int position, long id) {
         super.onListItemClick(l, v, position, id);
         setResult(RESULT_OK, ((TCBuildProxy)l.getItemAtPosition(position)).getData());
         finish();
     }
}
